package server.util.model;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import util.Task;

public class TaskListTableModel extends AbstractTableModel {

    public static final int //
            COL_PID = 0,
            COL_NAME = 1,
            COL_SESSION_NAME = 2,
            COL_SESSION_ID = 3,
            COL_MEMORY_USAGE = 4;

    private String[] columnNames = {"PID", "Name", "Session Name", "Session ID", "Memory Usage"};
    private List<Task> data;
    private JTable table;

    public TaskListTableModel() {
        super();

        data = new ArrayList<>();
    }
    
    public void setTable(JTable table) {
        this.table = table;
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
        Task task = data.get(row);

        switch (col) {
            case COL_PID:
                return task.pid;
            case COL_NAME:
                return task.name;
            case COL_SESSION_NAME:
                return task.sessionName;
            case COL_SESSION_ID:
                return task.sessionId;
            case COL_MEMORY_USAGE:
                return task.memoryUsage;
        }

        return "-";
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public void addTask(Task task) {
        data.add(task);
        fireDataChanged();
    }

    public void addTasks(boolean clear, List<Task> tasks) {
        if (clear) {
            clear();
        }

        for (Task task : tasks) {
            data.add(task);
        }
        
        fireDataChanged();
    }

    public void clear() {
        data.clear();
    }

    public Task getTaskAt(int row) {
        return data.get(row);
    }

    private void fireDataChanged() {
        this.fireTableDataChanged();

        for (int column = 0; column < table.getColumnCount(); column++) {
            TableColumn tableColumn = table.getColumnModel().getColumn(column);
            int preferredWidth = tableColumn.getMinWidth();
            int maxWidth = tableColumn.getMaxWidth();

            for (int row = 0; row < table.getRowCount(); row++) {
                TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
                Component c = table.prepareRenderer(cellRenderer, row, column);
                int width = c.getPreferredSize().width + table.getIntercellSpacing().width;
                preferredWidth = Math.max(preferredWidth, width);

                if (preferredWidth >= maxWidth) {
                    preferredWidth = maxWidth;
                    break;
                }
            }

            tableColumn.setPreferredWidth(preferredWidth);
        }
    }
    
}
